package com.blog.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 封装一个通用的JDBC工具类
 */
public class BaseDao {

    public Connection conn = null;
    public PreparedStatement ps = null;
    public ResultSet resultSet = null;

    static String driverClassName;
    static String url;
    static String username;
    static String password;

    static {
        //输入流
        try {
            InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties=new Properties();
            properties.load(inputStream);
            driverClassName=properties.getProperty("jdbc.driverClassName");
            url=properties.getProperty("jdbc.url");
            username=properties.getProperty("jdbc.username");
            password=properties.getProperty("jdbc.password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 01--获取数据库连接
     *
     * @return
     */
    public void getConn() {
        try {
            //加载驱动
            Class.forName(driverClassName);
            //获取连接对象
            conn = DriverManager.getConnection(url, username, password);
            //System.out.println("数据库连接对象:" + conn);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 02--关闭连接
     */
    public void closeConn(Connection conn, ResultSet resultSet, PreparedStatement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 03--执行查询
     * String sql="select * from tb_users where username=? and password=?";
     * Object[] parms={"刘备","123456"};
     *
     * @return
     */
    public ResultSet execQuery(String sql, Object[] parms) {
        try {
            //获取数据库连接
            this.getConn();
            ps = conn.prepareStatement(sql);
            if (parms != null) {
                for (int i = 0; i < parms.length; i++) {
                    ps.setObject(i + 1, parms[i]);
                }
            }
            resultSet = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }


    /**
     * 04-通用的执行增删改
     * String sql="update tb_users set password=? where username=?";
     * Object[] parms={"admin","刘备"};
     *
     * @return
     */
    public int execUpdate(String sql, Object[] parms) {
        int count = 0;
        try {
            this.getConn();
            ps = conn.prepareStatement(sql);
            if (parms != null) {
                for (int i = 0; i < parms.length; i++) {
                    ps.setObject(i + 1, parms[i]);
                }
            }
            count = ps.executeUpdate();
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭数据库连接
            this.closeConn(conn, resultSet, ps);
        }
        return count;
    }

    public static void main(String[] args) {
        try {
            BaseDao baseDao = new BaseDao();
            //baseDao.getConn();
            //            String sql="select * from tb_users where username=? and password=?";
            //            Object[] parms={"刘备","123406"};
            //            ResultSet resultSet = baseDao.execQuery(sql, parms);
            //            if(resultSet.next()){
            //                System.out.println("登录成功");
            //            }else{
            //                System.out.println("登录失败");
            //            }

            String sql = "update tb_users set password=? where username=?";
            Object[] parms = {"admin", "刘备"};
            int count = baseDao.execUpdate(sql, parms);
            System.out.println(count > 0 ? "密码修改成功" : "密码修改失败");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
